#include "MainWindow.h"
#include <QWebChannel>
#include <QTimer>
#include <QLabel>
#include "FrameLessHelper/utilities.h"
#include "FrameLessHelper/framelesswindowsmanager.h"

MainWindow::MainWindow(QWidget *parent) : QWebEngineView(parent)
{
	createWinId();
	SetupUi();
    handle = new WebPageHandle(this);
    auto* channel = new QWebChannel(this);
    channel->registerObject("handle", handle);
    this->page()->setWebChannel(channel);

    this->setContextMenuPolicy(Qt::NoContextMenu);

    setUrl(QUrl("qrc:/index.html"));
    for(int i = 0; i <20; i++)
    {
        QTimer::singleShot(2000 + i * 1000, this, [this, i](){
            emit(handle->DebugFunc(std::to_string(i).c_str()));
        });
    }
}

void MainWindow::SetupUi()
{
	auto* titleBar = new QWidget();
	titleBar->setFixedHeight(30);
	auto palette = new QPalette();
	palette->setColor(QPalette::Window, QColor(200, 200, 255));
	titleBar->setPalette(*palette);
	titleBar->setAutoFillBackground(true);
	titleBar->setLayout(new QHBoxLayout());
	minButton = new QPushButton("最小化", this);
	maxButton = new QPushButton("最大化", this);
	closeButton = new QPushButton("关闭", this);
	minButton->setFixedHeight(28);
	maxButton->setFixedHeight(28);
	closeButton->setFixedHeight(28);
	connect(minButton, &QPushButton::clicked, [this]()
	{
		showMinimized();
	});
	connect(maxButton, &QPushButton::clicked, [this]()
	{
		if (isMaximized() || isFullScreen())
			showNormal();
		else
			showMaximized();
	});
	connect(closeButton, &QPushButton::clicked, [this]()
	{
		close();
	});
	titleBar->setContentsMargins(0, 0, 0, 0);
	titleBar->layout()->setContentsMargins(0, 0, 0, 0);
	dynamic_cast<QHBoxLayout*>(titleBar->layout())->addWidget(new QLabel("这里将会显示窗口标题", this));
	dynamic_cast<QHBoxLayout*>(titleBar->layout())->addStretch();
	dynamic_cast<QHBoxLayout*>(titleBar->layout())->addWidget(minButton);
	dynamic_cast<QHBoxLayout*>(titleBar->layout())->addWidget(maxButton);
	dynamic_cast<QHBoxLayout*>(titleBar->layout())->addWidget(closeButton);
	layout()->addWidget(titleBar);
	layout()->setSpacing(0);
}


void MainWindow::showEvent(QShowEvent *event)
{
	QWebEngineView::showEvent(event);
	static bool inited = false;

	if (!inited)
	{
		const auto win = windowHandle();
		if (win)
		{
			FramelessWindowsManager::addWindow(win);
			FramelessWindowsManager::setTitleBarHeight(win, 30);
			FramelessWindowsManager::addIgnoreObject(win, minButton);
			FramelessWindowsManager::addIgnoreObject(win, maxButton);
			FramelessWindowsManager::addIgnoreObject(win, closeButton);
			inited = true;
		}
	}
}

void MainWindow::changeEvent(QEvent *event)
{
	QWebEngineView::changeEvent(event);
	bool shouldUpdate = false;
	if (event->type() == QEvent::WindowStateChange) {
		if (isMaximized() || isFullScreen()) {
			layout()->setContentsMargins(0, 0, 0, 0);
			maxButton->setText(tr("窗口化"));
		} else if (!isMinimized()) {
			layout()->setContentsMargins(1, 1, 1, 1);
			maxButton->setText(tr("最大化"));
		}
		shouldUpdate = true;
	} else if (event->type() == QEvent::ActivationChange) {
		shouldUpdate = true;
	}
	if (shouldUpdate) {
		update();
	}
}